package com.example.api.service.impl;

import com.example.api.mapper.SaleMapper;
import com.example.api.model.entity.Sale;
import com.example.api.service.SaleService;
import com.example.api.utils.DataTimeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

@Service
public class SaleServiceImpl implements SaleService {

    @Resource
    private SaleMapper saleMapper;

    @Override
    public Sale save(Sale sale) {
        // 如果是新记录，设置ID
        if (sale.getId() == null || sale.getId().isEmpty()) {
            sale.setId(UUID.randomUUID().toString());
            sale.setCreateAt(DataTimeUtil.getNowTimeString());
            saleMapper.insert(sale);
        }
        else {
            saleMapper.update(sale);
        }
        return sale;
    }

    @Override
    public List<Sale> findAll() {
        return saleMapper.findAll();
    }

    @Override
    public List<Sale> searchByCompanyorCommodityLike(String keyword) {
        return saleMapper.findAllByCompanyorCommodityLike("%" + keyword + "%");
    }

    @Override
    public void delete(String id) {
        saleMapper.delete(id);
    }
}
